package com.cn.yunhe.rentcars.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证图片
 * 
 * @author ZSL
 *
 */
public class AuthCodeImg {
    private Integer width = 70;
    private Integer height = 24;
    private BufferedImage bimg;
    private String randomCode;
    private Integer codeTop;// 文字上边距
    private Integer codeLeft = 4;

    public AuthCodeImg() {
    }

    public AuthCodeImg(Integer width, Integer height) {
        this.width = width;
        this.height = height;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public BufferedImage getBimg() {
        return bimg;
    }

    public String getRandomCode() {
        return randomCode;
    }

    public void setCodeTop(Integer codeTop) {
        this.codeTop = codeTop;
    }

    public AuthCodeImg init() {
        this.makeImg();
        return this;
    }

    /* 该方法主要作用是获得随机生成的颜色 */
    private Color getRandColor(int s, int e) {
        Random random = new Random();
        if (s > 255)
            s = 255;
        if (e > 255)
            e = 255;
        int r, g, b;
        r = s + random.nextInt(e - s); // 随机生成RGB颜色中的r值
        g = s + random.nextInt(e - s); // 随机生成RGB颜色中的g值
        b = s + random.nextInt(e - s); // 随机生成RGB颜色中的b值
        return new Color(r, g, b);
    }

    private void makeImg() {
        // 创建BufferedImage对象,其作用相当于一图片
        bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 创建Graphics对象,其作用相当于画笔
        Graphics graphics = bimg.getGraphics();
        // 创建Grapchics2D对象
        Graphics2D g2d = (Graphics2D) graphics;
        Random random = new Random();
        // 定义字体样式
        Font mfont = new Font("宋体", Font.BOLD, 16);
        graphics.setColor(getRandColor(200, 250));
        // 绘制背景
        graphics.fillRect(0, 0, width, height);
        // 设置字体
        graphics.setFont(mfont);
        graphics.setColor(getRandColor(180, 200));
        // 绘制100条颜色和位置全部为随机产生的线条,该线条为2f
        for (int i = 0; i < 100; i++) {
            int x = random.nextInt(width - 1);
            int y = random.nextInt(height - 1);
            int x1 = random.nextInt(6) + 1;
            int y1 = random.nextInt(12) + 1;
            // 定制线条样式
            BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
            Line2D line = new Line2D.Double(x, y, x + x1, y + y1);
            g2d.setStroke(bs);
            // 绘制直线
            g2d.draw(line);
        }
        this.randomCode(graphics, random);
        graphics.dispose(); // 释放g所占用的系统资源

    }

    private void randomCode(Graphics graphics, Random random) {
        // 输出由英文，数字，和中文随机组成的验证文字，具体的组合方式根据生成随机数确定。
        String sRand = "";
        String ctmp = "";
        int itmp = 0;
        random = new Random();
        // 制定输出的验证码为四位
        for (int i = 0; i < 4; i++) {
            switch (random.nextInt(3)) {
            case 1: // 生成A-Z的字母
            case 3:
                itmp = random.nextInt(26) + 65;
                ctmp = String.valueOf((char) itmp);
                break;
            default:
                itmp = random.nextInt(10) + 48;
                ctmp = String.valueOf((char) itmp);
                break;
            }
            sRand += ctmp;
            Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), random.nextInt(110));
            graphics.setColor(color);
            // 将生成的随机数进行随机缩放并旋转制定角度 PS.建议不要对文字进行缩放与旋转,因为这样图片可能不正常显示
            /* 将文字旋转制定角度 */
            Graphics2D g2d_word = (Graphics2D) graphics;
            AffineTransform trans = new AffineTransform();
            // trans.rotate((45) * 3.14 / 180, 15 * i + 8, 7);
            /* 缩放文字 */
            float scaleSize = random.nextFloat() + 0.8f;
            if (scaleSize > 1f)
                scaleSize = 1f;
            trans.scale(scaleSize, scaleSize);
            g2d_word.setTransform(trans);

            if (null == codeTop || 0 == codeTop) {
                codeTop = this.height / 2 - 10;
            }
            if (codeTop <= 12) {
                codeTop = 16;
            }
            graphics.drawString(ctmp, 15 * i + 10, codeTop);
            codeLeft += 15;
        }
        this.randomCode = sRand;
    }
}
